/*
 * BaseAction.java
 * 
 * Version information
 * 
 * April 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */

package net.amsoft.iservice.management.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.dataObject.LoginData;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * @author mohit
 * 
 */
public abstract class BaseAction extends Action {

    public static String F_PROCESS = "process(): ";
    public static String F_ENTRY = "entry";
    public final ActionForward execute(ActionMapping oMapping,
            ActionForm oForm, HttpServletRequest oRequest,
            HttpServletResponse oResponse) {

        preProcess(oMapping, oForm, oRequest, oResponse);
        ActionForward oForward = null;
        ActionMessages oMsgs = new ActionErrors();
        try {
            oForward = process(oMapping, oForm, oRequest, oResponse);
        } catch (Exception e) {
            if (e instanceof IServiceException) {
                IServiceException oISE = (IServiceException) e;
                oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(oISE
                        .getMessage(), false));
                saveErrors(oRequest, oMsgs);
                IServiceUtil.log(IServiceConstants.WARN, this, 
                        "execute(): " + oISE.getErrorCode() + "," + 
                        IServiceConstants.getError(oISE.getErrorCode()));
            } else {
                IServiceUtil.log(IServiceConstants.WARN, this, 
                        "execute(): Exception=" + e.getMessage());
            }
            e.printStackTrace();
            return oMapping.findForward(IBrokerConstants.FWD_CHKLOGIN);
        }
        postProcess(oMapping, oForm, oRequest, oResponse);
        return oForward;
    }

    public void preProcess(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse) {

        IServiceUtil.log(IServiceConstants.INFO, this, "preProcess(): entry");

        // get the existing session
        HttpSession ohSession = oRequest.getSession(false);
        LoginData oLoginData = (LoginData) ohSession
                .getAttribute(IBrokerConstants.SESSION_LOGIN_OBJ);
        if (oLoginData == null) {
            oLoginData = new LoginData();
            ohSession.setAttribute(IBrokerConstants.SESSION_LOGIN_OBJ,
                    oLoginData);
            IServiceUtil.log(IServiceConstants.INFO, this, "preProcess(): new LoginData created");
        }
    }// preProcess() ends

    public abstract ActionForward process(ActionMapping oMapping,
            ActionForm oForm, HttpServletRequest oRequest,
            HttpServletResponse oResponse) throws Exception;

    public void postProcess(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse) {
    }

}
